<template>
  <el-container class="top-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logOut" round>退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside :width="iscollapse?'64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-col>
          <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#1492e4"
            :unique-opened="true"
            :collapse="iscollapse"
            :collapse-transition="false"
            router
            :default-active="activePath"
          >
            <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
              <template slot="title">
                <i :class="iconObj[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <el-menu-item
                :index="'/'+subItem.path"
                v-for="subItem in item.children"
                :key="subItem.id"
                @click="handlePath('/'+subItem.path)"
              >
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{subItem.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-col>
      </el-aside>
      <!-- 右侧区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      iconObj: {
        '125': 'iconfont icon-users',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      iscollapse: false,
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    async logOut() {
      const congfirmlayout = await this.$confirm('确定退出吗?', '提示', {
        cancelButtonClass: 'btn-custom-cancel',
        customClass: 'myClass',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
        // center: true
      }).catch(err => err)
      console.log(congfirmlayout)
      if (congfirmlayout !== 'confirm') {
        return this.$message({
          type: 'info',
          message: '已取消退出'
        })
      }
      this.$message.success('您已退出')
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      // console.log(res)
    },
    toggleCollapse() {
      this.iscollapse = !this.iscollapse
    },
    handlePath(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.top-container {
  height: 100%;
}
.el-header {
  background-color: black;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  color: #fff;
  font-size: 20px;
  align-items: center;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 10px;
    }
  }
}
.iconfont {
  margin-right: 10px;
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: 0;
  }
}
.el-main {
  background-color: #eaedf1;
}

.toggle-button {
  text-align: center;
  line-height: 24px;
  font-size: 15px;
  background-color: #4a5064;
  color: #fff;
  cursor: pointer;
}
.btn-custom-cancel {
  float: right;
  margin-left: 10px;
}
</style>
